package com.dotto.atos.activity;

import io.card.payment.CardIOActivity;
import io.card.payment.CreditCard;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.dotto.atos.Config;
import com.dotto.atos.R;
import com.dotto.atos.fragments.AnnouncementsFragment;
import com.dotto.atos.fragments.BlueWikiFragment;
import com.dotto.atos.fragments.CardApplication1Fragment;
import com.dotto.atos.fragments.CardApplicationMenuFragment;
import com.dotto.atos.fragments.CheckOutFragment;
import com.dotto.atos.fragments.LoginFragment;
import com.dotto.atos.fragments.MenuFragment;
import com.dotto.atos.fragments.OnlinePaymentFragment;
import com.dotto.atos.fragments.RefusalPanelFragment;
import com.dotto.atos.fragments.ReportFragment;
import com.dotto.atos.services.actions.BranchService;
import com.dotto.atos.utils.TextUtil;

public class HomeActivity extends FragmentActivity {

	public CardApplication1Fragment cardApplication1Fragment;
	public CardApplicationMenuFragment cardApplicationMenuFragment;
	public ReportFragment reportFragment;
	public AnnouncementsFragment announcementsFragment;
	public BlueWikiFragment bluewikiFragment;
	public RefusalPanelFragment refusalPanelFragment;
	public CheckOutFragment checkOutFragment;
	public OnlinePaymentFragment onlinePaymentFragment;
	public LoginFragment loginFragment;
	public MenuFragment menuFragment;
	public static HomeActivity instance;
	public static boolean added;
	public static String backFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		instance = this;
		BranchService.init(this);
		if (findViewById(R.id.fragment_container) != null) {
			if (savedInstanceState != null) {
				return;
			}
			showLoginFragment(-1, getIntent().getExtras());
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}

	/*
	 * Change Fragments
	 */
	public void showCardApplication1Fragment(int type) {
		backFragment = CardApplicationMenuFragment.TAG;
		showFragment(getCardApplication1Fragment(), type,
				CardApplication1Fragment.TAG);
	}

	public void showCardApplication1Fragment(int type, Bundle bundle) {
		backFragment = CardApplicationMenuFragment.TAG;
		showFragment(getCardApplication1Fragment(bundle), type,
				CardApplication1Fragment.TAG);
	}

	public void showCardApplicationMenuFragment(int type) {
		backFragment = MenuFragment.TAG;
		showFragment(getCardApplicationMenuFragment(), type,
				CardApplicationMenuFragment.TAG);
	}

	public void showCardApplicationMenuFragment(int type, Bundle bundle) {
		backFragment = MenuFragment.TAG;
		showFragment(getCardApplicationMenuFragment(bundle), type,
				CardApplicationMenuFragment.TAG);
	}

	public void showReportFragment(int type) {
		backFragment = MenuFragment.TAG;
		showFragment(getReportFragment(), type, ReportFragment.TAG);
	}

	public void showReportFragment(int type, Bundle bundle) {
		backFragment = MenuFragment.TAG;
		showFragment(getReportFragment(bundle), type, ReportFragment.TAG);
	}

	public void showAnnouncementsFragment(int type) {
		backFragment = MenuFragment.TAG;
		showFragment(getAnnouncementsFragment(), type,
				AnnouncementsFragment.TAG);
	}

	public void showAnnouncementsFragment(int type, Bundle bundle) {
		backFragment = MenuFragment.TAG;
		showFragment(getAnnouncementsFragment(bundle), type,
				AnnouncementsFragment.TAG);
	}

	public void showBlueWikiFragment(int type) {
		backFragment = MenuFragment.TAG;
		showFragment(getBlueWikiFragment(), type, BlueWikiFragment.TAG);
	}

	public void showBlueWikiFragment(int type, Bundle bundle) {
		backFragment = MenuFragment.TAG;
		showFragment(getBlueWikiFragment(bundle), type, BlueWikiFragment.TAG);
	}

	public void showRefusalPanelFragment(int type) {
		backFragment = MenuFragment.TAG;
		showFragment(getRefusalPanelFragment(), type, RefusalPanelFragment.TAG);
	}

	public void showRefusalPanelFragment(int type, Bundle bundle) {
		backFragment = MenuFragment.TAG;
		showFragment(getRefusalPanelFragment(bundle), type,
				RefusalPanelFragment.TAG);
	}

	public void showCheckOutFragment(int type) {
		backFragment = OnlinePaymentFragment.TAG;
		showFragment(getCheckOutFragment(), type, CheckOutFragment.TAG);
	}

	public void showCheckOutFragment(int type, Bundle bundle) {
		backFragment = OnlinePaymentFragment.TAG;
		showFragment(getCheckOutFragment(bundle), type, CheckOutFragment.TAG);
	}

	public void showOnlinePaymentFragment(int type) {
		backFragment = MenuFragment.TAG;
		showFragment(getOnlinePayment(), type, OnlinePaymentFragment.TAG);
	}

	public void showOnlinePaymentFragment(int type, Bundle bundle) {
		backFragment = MenuFragment.TAG;
		showFragment(getOnlinePaymentFragment(bundle), type,
				OnlinePaymentFragment.TAG);
	}

	public void showLoginFragment(int type) {
		backFragment = null;
		showFragment(getLoginFragment(), type, LoginFragment.TAG);
	}

	public void showLoginFragment(int type, Bundle bundle) {
		backFragment = null;
		showFragment(getLoginFragment(bundle), type, LoginFragment.TAG);
	}

	public void showMenuFragment(int type) {
		backFragment = LoginFragment.TAG;
		showFragment(getMenuFragment(), type, MenuFragment.TAG);
	}

	public void showMenuFragment(int type, Bundle bundle) {
		backFragment = LoginFragment.TAG;
		showFragment(getMenuFragment(bundle), type, "");
	}

	public void showFragment(Fragment fragment, int type, String name) {
		if (fragment == null)
			return;
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

		if (type > 0) {
			ft.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter,
					R.anim.pop_exit);
		}
		ft.addToBackStack(name);
		if (!added) {
			added = true;
			ft.add(R.id.fragment_container, fragment).commit();
		} else
			ft.replace(R.id.fragment_container, fragment).commit();
	}

	public void showBackStack(String tag) {
		backFragment = null;
		getSupportFragmentManager().popBackStackImmediate(tag, 0);
	}

	/*
	 * UI Getter Setter
	 */

	public CardApplicationMenuFragment getCardApplicationMenuFragment() {
		return getCardApplicationMenuFragment(null);
	}

	public CardApplicationMenuFragment getCardApplicationMenuFragment(
			Bundle bundle) {
		if (cardApplicationMenuFragment == null) {
			cardApplicationMenuFragment = new CardApplicationMenuFragment();
			if (bundle != null)
				cardApplicationMenuFragment.setArguments(bundle);
		}
		return cardApplicationMenuFragment;
	}

	public CardApplication1Fragment getCardApplication1Fragment() {
		return getCardApplication1Fragment(null);
	}

	public CardApplication1Fragment getCardApplication1Fragment(Bundle bundle) {
		if (cardApplication1Fragment == null) {
			cardApplication1Fragment = new CardApplication1Fragment();
			if (bundle != null)
				cardApplication1Fragment.setArguments(bundle);
		}
		return cardApplication1Fragment;
	}

	public ReportFragment getReportFragment() {
		return getReportFragment(null);
	}

	public ReportFragment getReportFragment(Bundle bundle) {
		if (reportFragment == null) {
			reportFragment = new ReportFragment();
			if (bundle != null)
				reportFragment.setArguments(bundle);
		}
		return reportFragment;
	}

	public AnnouncementsFragment getAnnouncementsFragment() {
		return getAnnouncementsFragment(null);
	}

	public AnnouncementsFragment getAnnouncementsFragment(Bundle bundle) {
		if (announcementsFragment == null) {
			announcementsFragment = new AnnouncementsFragment();
			if (bundle != null)
				announcementsFragment.setArguments(bundle);
		}
		return announcementsFragment;
	}

	public BlueWikiFragment getBlueWikiFragment() {
		return getBlueWikiFragment(null);
	}

	public BlueWikiFragment getBlueWikiFragment(Bundle bundle) {
		if (bluewikiFragment == null) {
			bluewikiFragment = new BlueWikiFragment();
			if (bundle != null)
				bluewikiFragment.setArguments(bundle);
		}
		return bluewikiFragment;
	}

	public RefusalPanelFragment getRefusalPanelFragment() {
		return getRefusalPanelFragment(null);
	}

	public RefusalPanelFragment getRefusalPanelFragment(Bundle bundle) {
		if (refusalPanelFragment == null) {
			refusalPanelFragment = new RefusalPanelFragment();
			if (bundle != null)
				refusalPanelFragment.setArguments(bundle);
		}
		return refusalPanelFragment;
	}

	public CheckOutFragment getCheckOutFragment() {
		return getCheckOutFragment(null);
	}

	public CheckOutFragment getCheckOutFragment(Bundle bundle) {
		if (checkOutFragment == null) {
			checkOutFragment = new CheckOutFragment();
			if (bundle != null)
				checkOutFragment.setArguments(bundle);
		}
		return checkOutFragment;
	}

	public OnlinePaymentFragment getOnlinePayment() {
		return getOnlinePaymentFragment(null);
	}

	public OnlinePaymentFragment getOnlinePaymentFragment(Bundle bundle) {
		if (onlinePaymentFragment == null) {
			onlinePaymentFragment = new OnlinePaymentFragment();
			if (bundle != null)
				onlinePaymentFragment.setArguments(bundle);
		}
		return onlinePaymentFragment;
	}

	public LoginFragment getLoginFragment() {
		return getLoginFragment(null);
	}

	public LoginFragment getLoginFragment(Bundle bundle) {
		if (loginFragment == null) {
			loginFragment = new LoginFragment();
			if (bundle != null)
				loginFragment.setArguments(bundle);
		}
		return loginFragment;
	}

	public MenuFragment getMenuFragment() {
		return getMenuFragment(null);
	}

	public MenuFragment getMenuFragment(Bundle bundle) {
		if (menuFragment == null) {
			menuFragment = new MenuFragment();
			if (bundle != null)
				menuFragment.setArguments(bundle);
		}
		return menuFragment;
	}

	@Override
	public void onBackPressed() {
		if (TextUtil.isEmpty(backFragment))
			return;
		if (backFragment == LoginFragment.TAG) {
			showBackStack(LoginFragment.TAG);
		} else if (backFragment == MenuFragment.TAG) {
			showBackStack(MenuFragment.TAG);
			backFragment = LoginFragment.TAG;
		} else if (backFragment == OnlinePaymentFragment.TAG) {
			showBackStack(OnlinePaymentFragment.TAG);
			backFragment = MenuFragment.TAG;
		} else if (backFragment == CheckOutFragment.TAG) {
			showBackStack(CheckOutFragment.TAG);
			backFragment = OnlinePaymentFragment.TAG;
		} else if (backFragment == RefusalPanelFragment.TAG) {
			showBackStack(RefusalPanelFragment.TAG);
			backFragment = MenuFragment.TAG;
		} else if (backFragment == BlueWikiFragment.TAG) {
			showBackStack(BlueWikiFragment.TAG);
			backFragment = MenuFragment.TAG;
		} else if (backFragment == AnnouncementsFragment.TAG) {
			showBackStack(AnnouncementsFragment.TAG);
			backFragment = MenuFragment.TAG;
		} else if (backFragment == ReportFragment.TAG) {
			showBackStack(ReportFragment.TAG);
			backFragment = MenuFragment.TAG;
		} else if (backFragment == CardApplicationMenuFragment.TAG) {
			showBackStack(CardApplicationMenuFragment.TAG);
			backFragment = MenuFragment.TAG;
		} else if (backFragment == CardApplication1Fragment.TAG) {
			showBackStack(CardApplication1Fragment.TAG);
			backFragment = CardApplicationMenuFragment.TAG;
		}
	}

	public void hideKeyboard(EditText editText) {
		if (editText != null) {
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
		}
	}

	// Card IO

	public void scanCard() {
		Intent scanIntent = new Intent(this, CardIOActivity.class);
		scanIntent.putExtra(CardIOActivity.EXTRA_APP_TOKEN,
				Config.CARDIO_SCAN_TOKEN);
		scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, true);
		scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, false);
		scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_ZIP, false);
		this.startActivityForResult(scanIntent, Config.CARDIO_SCAN_REQUEST_CODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == Config.CARDIO_SCAN_REQUEST_CODE) {
			String resultStr;

			if (data != null && data.hasExtra(CardIOActivity.EXTRA_SCAN_RESULT)) {
				CreditCard scanResult = data
						.getParcelableExtra(CardIOActivity.EXTRA_SCAN_RESULT);
				resultStr = scanResult.cardNumber;
			} else {
				resultStr = null;
			}
			if (checkOutFragment != null)
				checkOutFragment.onCardReaded(resultStr);
		}
	}

}
